        <div class="box">
            <div class="title">
                <h5><?=$title?></h5>
            </div>
            
            <div id="chartdiv" style="width: 100%; height: 400px;"></div>
            <table>
                <tr>
                    <td>
                        <input type="radio" checked="true" name="group" id="rb1" onclick="setLabelPosition()"/> 
                        labels outside
                        <input type="radio" name="group" id="rb2" onclick="setLabelPosition()"/>
                        labels inside
                    </td>
                    <td>
                        <input type="radio" name="group2" id="rb3" onclick="set3D()"/>
                        3D
                        <input type="radio" checked="true" name="group2" id="rb4" onclick="set3D()"/>
                        2D
                    </td>
                    <td>
                        Legend switch type:
                        <input type="radio" checked="true" name="group3" id="rb5" onclick="setSwitch()"/> 
                        x
                        <input type="radio" name="group3" id="rb6" onclick="setSwitch()"/>
                        v
                    </td>
                </tr>
            </table>
        </div>
        

    <link rel="stylesheet" href="amcharts/style.css" type="text/css" />
    <script src="amcharts/javascript/amcharts.js" type="text/javascript"></script>
    <script src="amcharts/javascript/raphael.js" type="text/javascript"></script>   
    <script type="text/javascript">
         // declaring variables
        	var chart;
            var legend;
            var dataProvider;
			
             
             function loadCSV(file) {
			 
             if (window.XMLHttpRequest) {
                 // IE7+, Firefox, Chrome, Opera, Safari
                 var request = new XMLHttpRequest();
             }
             else {
                 // code for IE6, IE5
                 var request = new ActiveXObject('Microsoft.XMLHTTP');
             }
             // load
             request.open('GET', file, false);
             request.send();
			 
             parseCSV(request.responseText);
         }
          // method which parses csv data
         function parseCSV(data){ 
             
			   data = data.replace (/\r\n/g, "\n");
              data = data.replace (/\r/g, "\n");
              var rows = data.split("\n");
              dataProvider = [];
              // loop through all rows
             for (var i = 0; i < rows.length; i++){
                 // this line helps to skip empty rows
                 if (rows[i]) {                    
                      var column = rows[i].split("|");  
                      var header = column[0];
                      var rencana = column[1];
                      var realisasi = column[2];
                      var sisa = column[3];
					 var rencana_bal = column[4];
                      var realisasi_bal = column[5];
                      var sisa_bal = column[6];
                      var dataObject = {header:header, rencana:rencana, realisasi:realisasi, sisa:sisa, rencana_bal:rencana_bal, realisasi_bal:realisasi_bal, sisa_bal:sisa_bal};
                      dataProvider.push(dataObject);
                 }
             }
              // set data provider to the chart
             //chart.dataProvider = dataProvider;
              // this will force chart to rebuild using new data            
             //chart.validateData();
          }
		 
			window.onload = function() 
            {         
             	loadCSV("<?=base_url();?>data.txt"); 

				chart = new AmCharts.AmPieChart();				
                chart.dataProvider = dataProvider;				
                chart.titleField = "header";
                chart.valueField = "rencana";

                legend = new AmCharts.AmLegend();
                legend.align = "center";
                legend.markerType = "circle";
                chart.addLegend(legend); 
                chart.write("chartdiv");
            }
        
        
            function setLabelPosition()
            {
               	if(document.getElementById("rb1").checked)
               	{
                chart.labelRadius = 30;
                chart.labelText = "[[title]]: [[value]]";
                }
                else
                {
            	   chart.labelRadius = -30;
            	   chart.labelText = "[[percents]]%";
                }
            	chart.validateNow();
            }
            
            
            function set3D()
            {        
            	if(document.getElementById("rb3").checked)
            	{
            		chart.depth3D = 10;
            		chart.angle = 10;
            	}
            	else
            	{
            		chart.depth3D = 0;
            		chart.angle = 0;
            	}
            	chart.validateNow();
            }
            
            function setSwitch()
            {        
                if(document.getElementById("rb5").checked)
                {
                    legend.switchType = "x";
                }
                else
                {
            	   legend.switchType = "v";
            	}
            	legend.validateNow();
            }

        </script>
    
    
    <?php 
$terang = true;
$jenis=1; //rekap
$type=2; //month to date
$beban=2; //by dept
$coa="-";//coa==all
$department="-";
  
$bln=date("m");
$thn=date("Y");
$bulan=$bln;
  if($beban=="2"){
	$sql_dept="select distinct DeptID from budgetset where Tahun='$thn' and Bulan='$bln' order by DeptID";
	$query_dept=mysql_query($sql_dept);
	$x=0; 
	while($row_dept=mysql_fetch_array($query_dept)){
		$anggaranh=0; $realh=0; $anggaranh_thn=0; $realh_thn=0; 
		$anggaran1=0; $real1=0; $anggaran_thn=0; $real_thn=0; 
		$anggaran0=0; $real0=0;  $anggaran_thn0=0; $real_thn0=0; 
		
 		$sql_budget="select * from budget where Kelompok='BUDGET' order by NoUrut";
 		$query_budget=mysql_query($sql_budget);
		while($row_budget=mysql_fetch_array($query_budget)){
			if($row_budget['Jenis']=="D"){
				if($type=="1"){ //current month
					$sql_bs="select * from budgetset where Tahun='$thn' and Bulan='$bln' and MataAnggaran='$row_budget[MataAnggaran]' and DeptID='$row_dept[DeptID]'";
				}else{ //month to date
					$sql_bs="select sum(Anggaran) as t_anggaran, sum(Realisasi) as t_realisasi from budgetset where Tahun='$thn' and Bulan<='$bulan' and MataAnggaran='$row_budget[MataAnggaran]' and DeptID='$row_dept[DeptID]'";
				}
 				$query_bs=mysql_query($sql_bs);			
				while($row_bs=mysql_fetch_array($query_bs)){
					if($type=="1"){						 
						if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
							$anggaran1=$anggaran1+$row_bs['Anggaran'];
							$real1=$real1+$row_bs['Realisasi'];		
							$anggaran0=$row_bs['Anggaran'];
							$real0=$row_bs['Realisasi'];			
 						} 					
					}else{
						if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
							$anggaran1=$anggaran1+$row_bs['t_anggaran'];
							$real1=$real1+$row_bs['t_realisasi'];				
							$anggaran0=$row_bs['t_anggaran'];
							$real0=$row_bs['t_realisasi'];
						}//end if coa						
					}	
				}//end while row_bs
				
				$sql_tahun="select sum(Anggaran) as total_anggaran, sum(Realisasi) as total_realisasi from budgetset where Tahun='$thn' and MataAnggaran='$row_budget[MataAnggaran]' and DeptID='$row_dept[DeptID]'";
				$query_tahun=mysql_query($sql_tahun);
				while($row_tahun=mysql_fetch_array($query_tahun)){
					if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
						$anggaran_thn=$anggaran_thn+$row_tahun['total_anggaran'];
						$real_thn=$real_thn+$row_tahun['total_realisasi'];
						
						$anggaran_thn0=$row_tahun['total_anggaran'];
						$real_thn0=$row_tahun['total_realisasi'];
					}//end if coa
					
				}
				
				if($anggaran_thn0!=0 || $real_thn0!=0){ 
 					
					if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
						$anggaranh=$anggaranh+$anggaran0;
						$realh=$realh+$real0;
						$anggaranh_thn=$anggaranh_thn+$anggaran_thn0;
						$realh_thn=$realh_thn+$real_thn0;
					}//end if coa
					
 				}				
			}//end if Jenis='D'
			else if($row_budget['Jenis']=="H1"){				
				$temp_index=$row_budget['NoUrut'];
			}
			else if($row_budget['Jenis']=="H2"){
 				$temp_index=$row_budget['NoUrut'];
 			}
			else if($row_budget['Jenis']=="H3"){
 				$temp_index=$row_budget['NoUrut'];
 			}
			else if($row_budget['Jenis']=="H4"){
 				$temp_index=$row_budget['NoUrut'];
 			}
			else if($row_budget['Jenis']=="T1"){
 				$temp_index="";
 			}//end if jenis=T1
			else if($row_budget['Jenis']=="T2"){
 				$temp_index="";
 			}//end if jenis=T2
			else if($row_budget['Jenis']=="T3"){
 				$temp_index="";
 			}//end if jenis=T3
			else if($row_budget['Jenis']=="T4"){
 				$temp_index="";
 			}//end if jenis=T4
			else if($row_budget['Jenis']=="TH"){
  				$temp_index="";			 
			} //end else TH
 
		}//end while row_budget
		if($anggaranh!=0 || $realh!=0){
			$dept_header[$x]=$row_dept['DeptID'];
			$dept_anggaran[$x]=$anggaranh;
			$dept_real[$x]=$realh;
			$dept_anggaran_thn[$x]=$anggaranh_thn;
			$dept_real_thn[$x]=$realh_thn;
						
			$x++; 
		}
		
	}//end row while dept
	
	//begin sorting data
	$max_x=$x-1;
	for($w=0;$w<=$x;$w++){
		for($y=$max_x; $y>$w; $y--){
			if($dept_anggaran[$y] > $dept_anggaran[$y-1]){
				$tampung_header=$dept_header[$y];
				$tampung_anggaran=$dept_anggaran[$y];
				$tampung_real=$dept_real[$y];
				$tampung_anggaran_thn=$dept_anggaran_thn[$y];
				$tampung_real_thn=$dept_real_thn[$y];
				
				$dept_header[$y]=$dept_header[$y-1];
				$dept_header[$y-1]=$tampung_header;
				
 				$dept_anggaran[$y]=$dept_anggaran[$y-1];
				$dept_anggaran[$y-1]=$tampung_anggaran;
				
				$dept_real[$y]=$dept_real[$y-1];
				$dept_real[$y-1]=$tampung_real;
				
				$dept_anggaran_thn[$y]=$dept_anggaran_thn[$y-1];
				$dept_anggaran_thn[$y-1]=$tampung_anggaran_thn;
				
				$dept_real_thn[$y]=$dept_real_thn[$y-1];
				$dept_real_thn[$y-1]=$tampung_real_thn;
			}
		}
	}//end for sorting data	dept
	
    $data = "";
	for($l=0;$l<5;$l++){
		$ang_0=number_format($dept_anggaran[$l],2,',','.');
		$ang0=" ".$ang_0." ";
		$real_0=number_format($dept_real[$l],2,',','.');
		$real0=" ".$real_0." ";
		$sisa_00=$dept_anggaran[$l]-$dept_real[$l];
		$sisa_0=number_format($sisa_00,2,',','.');
		$sisa0=" ".$sisa_0." ";
		$total=$dept_anggaran[$l]+$dept_real[$l]+$sisa_00;
		if($total!=0){
			$ang=($dept_anggaran[$l]/$total)*100;
			$ang=round($ang,2);
			$real=($dept_real[$l]/$total)*100;
			$real=round($real,2);
			$sisa=($sisa_00/$total)*100;
			$sisa=round($sisa,2);
			if(($ang0!=0 && $ang0!="") || ($real0!=0 && $real0!="") || ($sisa0!=0 && $sisa0!="")){
                $data.= $dept_header[$l]."|".$ang."|".$real."|".$sisa."|".$ang0."|".$real0."|".$sisa0."\n";	
			}
		}
	}
		
	$FileName ="data.txt";
	$FileHandle = fopen($FileName, 'w') or die("can't open file");
				
	fwrite($FileHandle, $data);
	fclose($FileHandle);
}//end if beban dept
?> 